#pragma once
#include <muduo/net/http/HttpRequest.h>
#include <muduo/net/http/HttpResponse.h>
#include "../Middleware.hpp"
#include "CorsConfig.hpp"
namespace http
{
    namespace middleWare
    {
        class CorsMiddleware : public Middleware
        {
        public:
            explicit CorsMiddleware(const CorsConfig &config = CorsConfig::defaultConfig());

            void before(const HttpRequest &request) override;
            void after(HttpResponse &response) override;
            std::string join(const std::vector<std::string> &strings, const std::string &delimiter);//将字符串数组串联成一个字符串

            private:
            bool isOriginAllowed(const std::string &origin) const;//检查请求的源是否在允许的源中
            void handlePreflight(const HttpRequest &request, HttpResponse &response);//处理预检请求
            void addCorsHeaders(HttpResponse &response,const std::string &origin);//添加CORS响应头
        private:
            CorsConfig config;//跨域配置
        };
    }
}